/*
 * Copyright (C) 2011 Dirk Weißenborn, dirk.weissenborn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weissenborn.colabtex.server.tex;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;

import com.weissenborn.colabtex.server.etherpad.EtherpadUtils;

/**
 * The Class TexUtil is a helper for accessing the linux compiler and managing the output of files
 */
public class TexUtil {
	
	/**
	 * Delete group folder.
	 *
	 * @param groupId the group id
	 */
	public static void deleteGroupFolder(String groupId) {
		File f = new File(getDirectory4Group(groupId));
		f.delete();
	}
	
	/**
	 * Write pad to pdf.
	 *
	 * @param padId the pad id
	 * @return the string
	 */
	public static String writePadToPdf(String padId) {
		EtherpadUtils utils =  new EtherpadUtils("etherpad.ini");
		
		String text = utils.getText(padId);
		
		String padName = padId.split("[^a-zA-Z0-9.]")[1];
		String cPath =".";
		try {
			cPath = getDirectory(padId);
			File dir = new File(cPath);
			dir.mkdirs();
			BufferedWriter out = new BufferedWriter(new FileWriter(cPath+"/"+padName+".tex"));
			out.write(text);			
			out.close();
			
			String[] cmd = {
					"sh",
					"-c",
					"echo \"x\" | latex "+padName+".tex && " +
					"echo \"x\" | latex "+padName+".tex && " +
					"dvipdf "+padName+".dvi && " +
					"rm -rf *.aux *.dvi *.toc *.out *~"
					};
			
			final Process child = Runtime.getRuntime().exec(cmd,null,dir);
			
			Timer timer = new Timer();			
			
			TimerTask tt = new TimerTask() {
				@Override
				public void run() {
					child.destroy();					
				}
			};
			
			timer.schedule(tt, 60000);
			
			child.waitFor();
		}
		catch (IOException e) {
				e.printStackTrace();
		} catch (InterruptedException e) {			
			e.printStackTrace();
		}		
		
		
		return cPath+"/"+padName+".pdf";
	}

	/**
	 * Gets the directory.
	 *
	 * @param padId the pad id
	 * @return the directory
	 */
	public static String getDirectory(String padId) {
		//String padName = padId.split("[^a-zA-Z0-9.]")[1];
		String groupId = padId.split("[^a-zA-Z0-9.]")[0];
		String path = "/home/colabtex";
		return path+ "/groups/"+groupId;
	}
	
	/**
	 * Gets the directory4 group.
	 *
	 * @param groupId the group id
	 * @return the directory4 group
	 */
	public static String getDirectory4Group(String groupId) {
		String path = "/home/colabtex";
		return path+ "/groups/"+groupId;
	}
}
